package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.exception.GuiguException;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2024-03-14-11:51
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    SysMenuMapper sysMenuMapper;


    @Override
    public List<SysMenu> listNestedMenus() {

        List<SysMenu> sysMenuAll = sysMenuMapper.selectAll();

        List<SysMenu> parents = sysMenuAll.stream()
                .filter(sysMenu -> sysMenu.getParentId().longValue() ==0)
                .collect(Collectors.toList());

        buildMenuTree(parents,sysMenuAll);

        return parents;
    }

    @Override
    public void DeleteMenus(Long id) {
        List<SysMenu> children = sysMenuMapper.selectChildById(id);
        if (children.isEmpty()){
            sysMenuMapper.DeleteMenusById(id);
        }else {
            throw new GuiguException(ResultCodeEnum.MENU_DELETE_ERROR);
        }

    }

    @Override
    public void saveMenu(SysMenu sysMenu) {
        sysMenuMapper.saveMenu(sysMenu);
    }

    @Override
    public SysMenu FindMenuById(Long id) {
        SysMenu sysMenu = sysMenuMapper.FindMenuById(id);
        return sysMenu;
    }

    @Override
    public void UpdateMenu(SysMenu sysMenu) {
        Long count = sysMenuMapper.selectCountByTitle(sysMenu);
        if (count>0){
            throw new GuiguException(ResultCodeEnum.MENU_TITLE_EXIST_ERROR);
        }
        sysMenuMapper.UpdateMenu(sysMenu);

    }

    @Override
    public Map<String, Object> getMenusAndAssignedMenuIds(Long id) {

       List<SysMenu> sysMenus = sysMenuMapper.selectAll().stream().filter(sysMenu -> sysMenu.getStatus().intValue() ==1)
                .collect(Collectors.toList());
       List<Long> checkMenuIds = sysMenuMapper.selectByRoleId(id);
       List<SysMenu> parents = sysMenus.stream().filter(sysMenu -> sysMenu.getParentId().longValue() == 0)
               .collect(Collectors.toList());
       buildMenuTree(parents,sysMenus);
       Map<String,Object> map = new HashMap<>();
       map.put("menus",parents);
       map.put("menuIds",checkMenuIds);
       return map;
    }

    @Override
    public List<SysMenuVo> getMenus() {
        Long userid = LoginAuthInterceptor.getSysUser().getId();
        List<SysMenu> sysMenus = sysMenuMapper.getMenus(userid);

        List<SysMenu> parents = sysMenus.stream().filter(sysMenu -> sysMenu.getParentId().longValue() == 0)
                .collect(Collectors.toList());
        buildMenuTree(parents,sysMenus);

        List<SysMenuVo> sysMenuVos = parents.stream().map(parent ->{
         SysMenuVo sysMenuVo = new SysMenuVo();
         sysMenuVo.setName(parent.getComponent());

         List<SysMenu> children = parent.getChildren();
         if (!CollectionUtils.isEmpty(children)){
             List<SysMenuVo> sysMenuVOChildren = children.stream().map(child->{
                 SysMenuVo sysMenuVoc = new SysMenuVo();
                 sysMenuVoc.setName(child.getComponent());
                 return sysMenuVoc;
             }).collect(Collectors.toList());
             sysMenuVo.setChildren(sysMenuVOChildren);
         }
         return sysMenuVo;
        }).collect(Collectors.toList());
        return sysMenuVos;
    }

    private void buildMenuTree(List<SysMenu> parents,List<SysMenu> sysMenus){

        if (CollectionUtils.isEmpty(parents)){
            return;
        }

        parents.forEach(parent ->{
            List<SysMenu> children = sysMenus.stream()
                    .filter(sysMenu -> sysMenu.getParentId().longValue() == parent.getId().longValue())
                    .collect(Collectors.toList());

            if (!children.isEmpty()){
                parent.setChildren(children);
                buildMenuTree(children,sysMenus);
            }
        });
    }
}
